package com.example.learnvideo.ui

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.learnvideo.R
import com.example.learnvideo.base.BaseActivity
import com.example.learnvideo.databinding.ActivityFloatWindowBinding
import com.example.learnvideo.service.StockService
import com.example.learnvideo.util.AuthorityUtil

class FloatWindowActivity : BaseActivity<ActivityFloatWindowBinding>() {


    override fun ActivityFloatWindowBinding.initBinding() {

        //打开股指悬浮窗
        btnStockOpen.setOnClickListener{
            val intent = Intent(this@FloatWindowActivity,StockService::class.java)
            intent.putExtra("type",StockService.OPEN)
            startService(intent)
        }

        //关闭股指悬浮窗
        btnStockClose.setOnClickListener {
            val intent = Intent(this@FloatWindowActivity,StockService::class.java)
            intent.putExtra("type",StockService.CLOSE)
            startService(intent)
        }

        // AppOpsManager.OP_SYSTEM_ALERT_WINDOW是隐藏变量（值为24），不能直接引用
        // 未开启悬浮窗权限
        if(!AuthorityUtil.checkOp(this@FloatWindowActivity,24)){
            Toast.makeText(this@FloatWindowActivity,"请先给该应用开启悬浮窗权限",Toast.LENGTH_SHORT).show()
            // 跳到悬浮窗权限的设置页面
            AuthorityUtil.requestAlertWindowPermission(this@FloatWindowActivity)
        }
    }

    // 从悬浮窗权限的设置页面返回时触发
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AuthorityUtil.REQUEST_CODE){}
    }
}